
extern "C" {
#include <project.h>

    /** /#define DEBUG_SPCOMMMSH*/
#ifdef DEBUG_SPCOMMMSH
#define DEBUG_95
#endif

    //#include "ProtoIntf.h"
#include "sizes.h"
#include "comm.h"
#include "transact.h"
#include "msg.h"

    extern int hClock;
    extern COMM_OBJECT obCommunication;     /* Global variable in module comm.c , needed to initialise*/
    //static TRANSACTION_OBJECT *pobTran;

#include <date.h>
#include <tct.h>
#include <operate.h>

#ifdef  COUNTER_DEFINED
#include <counter.h>
#include <pcounter.h>
#endif

#include <define.h>
#include <hdt.h>
#include <gds.h>
#include <msgfuncs.h>
#ifdef EMV_MODULE
#include <emvfuncs.h>
#endif
#ifdef VXEMVAPP
/* No need to include here at this moment */
/* include <vxemvap.h>*/
#include <Funcids.h>
#include <mdlfuncs.h>
#include <EMVGlobals.hpp>
#include "..\..\Hostsrc\EMVsrc\EMVids.h"
#endif /* VXEMVAPP */
#include <tblutil.h>
#include <mht.h>

#include <FuncComms.h>
}


#include <CommsHandlerAll.h>

#include <Project.hpp>

#ifdef VXEMVAPP
extern long lnErrorMessage;
#endif /* VXEMVAPP */
/*********************************************************************************************
* @func int | inBuildAndSendPacket |
*
*
* @parm TRANSACTION_OBJECT *| pobTran |
*       Transaction object
*
* @rdesc return values.
* @flag int |  VS_SUCCESS, VS_ERR
* @end
**********************************************************************************************/
int inBuildAndSendPacket(TRANSACTION_OBJECT* pobTran) {
    SoftPayBaseCommsHandler* comms;
    int retVal;

#ifdef  COUNTER_DEFINED
    COUNTER_OBJECT obCounter;
    long lnTimer     ;

    obCounter.inIndex = inGetCounterIndex();
    inSetTimerOnOff(START_TIMER_2);

#endif  /* COUNTER_DEFINED */

    /** /pdebug (("--inBuildAndSendPacket--"));*/

    vdSGErrorMessage(0L);         /* Clear error message index */
    vdDisplayMessageFromFile(PROCESSING_MSG21, STATUS_MSG);


#ifdef VXEMVAPP
if(pobTran->srBRec.inChipStatus == EMV_CARD)
{

     if(pobTran->inEMVCardDecision == EMV_OFFLINE_AUTH)
    {

    	pobTran->srTRTRec.Offline = VS_TRUE;
    	pobTran->srBKey.fOffline = VS_TRUE;
    }
    else if (pobTran->inEMVCardDecision == EMV_VOICE_REFERRAL)
    {

        pobTran->srTRTRec.Offline = VS_TRUE;
        pobTran->srBKey.fOffline = VS_TRUE;

        if (inIncSTAN() != VS_SUCCESS)
	        return(VS_ERR);
    	 pobTran->srBRec.lnSTAN = lnGetSTAN();

        return VS_SUCCESS;
    }
    else if (pobTran->inEMVCardDecision == EMV_GO_ONLINE)
    {

        pobTran->srTRTRec.Offline = VS_FALSE;
        pobTran->srBKey.fOffline = VS_FALSE;

        if (pobTran->srBRec.inCode == SALE_OFFLINE) //jrd 05282005
		{

		    pobTran->srTRTRec.Offline = VS_TRUE;
		    pobTran->srBKey.fOffline = VS_TRUE;
    	}
    }


}
#endif

    if (inIncSTAN() != VS_SUCCESS)
        return(VS_ERR);
    pobTran->srBRec.lnSTAN = lnGetSTAN();


    comms = (SoftPayBaseCommsHandler *) getCommsHandler();
    comms->supplyPobTran(pobTran);

    if (comms->attemptOutwardCommunication() != TRUE)
        retVal = VS_ERR;
    else
        retVal = VS_SUCCESS;

    /**/pdebug(("--inBuildAndSendPacket OK--"));/**/

    //For EMV transactions
    #ifdef VXEMVAPP
	if(pobTran->srBRec.inChipStatus == EMV_CARD)
	{
		if((pobTran->srBRec.inTransactionResult == FAILED_TO_CONNECT) && (lnErrorMessage != CONNECT_FAILED))
		{
			//jrd 0572005
			/*vdSGRespMsg("");
			vdSGErrorMessage(0L);
			lnErrorMessage = CONNECT_FAILED;
			retVal =  VS_SUCCESS;*/
		}
	}
     #endif /* VXEMVAPP */
    return retVal;
} /* end func inBuildAndSendPacket() */

